﻿using System;
using BizElements.BusinessLayer;

namespace BizElements.BusinessLayer.Aspects
{
    #region Documentation.
    /// <summary>
    /// Contains configuration for cache aspect.
    /// </summary>
    /// <remarks>Settings are read from the application's config file. The following are the default settings:
    /// <code>
    /// <configuration>
    ///     <appSettings>
    ///         <add key="BizElements.BusinessLayer.Aspects.CacheAspectConfiguration.KeyGenerator.Assembly" value="BizElements.BusinessLayer.Aspects" />
    ///         <add key="BizElements.BusinessLayer.Aspects.CacheAspectConfiguration.KeyGenerator.Class" value="BizElements.BusinessLayer.Aspects.CacheKeyGenerator"/>
    ///         <add key="BizElements.BusinessLayer.Aspects.CacheAspectConfiguration.CategoryGenerator.Assembly" value="BizElements.BusinessLayer.Aspects" />
    ///         <add key="BizElements.BusinessLayer.Aspects.CategoryGenerator.KeyGenerator.Class" value="BizElements.BusinessLayer.Aspects.CacheCategoryGenerator"/>
    ///     </appSettings>
    /// </configuration>
    /// </code>
    /// </remarks>
    #endregion
    public static class CacheAspectConfiguration
    {
        /// <summary>
        /// Gets <see cref="ICacheCategoryGenerator"/> used by attributes dealing with cache aspect.
        /// </summary>
        public static ICacheKeyGenerator KeyGenerator { get; private set; }

        /// <summary>
        /// Gets <see cref="ICacheKeyGenerator"/> used by attributes dealing with cache aspect.
        /// </summary>
        public static ICacheCategoryGenerator CategoryGenerator { get; private set; }

        static CacheAspectConfiguration()
        {
            KeyGenerator = ReflectionUtil.TryCreateInstanceFromAppSettings("BizElements.BusinessLayer.Aspects.CacheAspectConfiguration.KeyGenerator.Assembly", "BizElements.BusinessLayer.Aspects.CacheAspectConfiguration.KeyGenerator.Class") as ICacheKeyGenerator;
            if (KeyGenerator == null)
                KeyGenerator = ReflectionUtil.TryCreateInstance("BizElements.BusinessLayer.Aspects", "BizElements.BusinessLayer.Aspects.CacheKeyGenerator") as ICacheKeyGenerator;


            CategoryGenerator = ReflectionUtil.TryCreateInstanceFromAppSettings("BizElements.BusinessLayer.Aspects.CacheAspectConfiguration.CategoryGenerator.Assembly", "BizElements.BusinessLayer.Aspects.CategoryGenerator.KeyGenerator.Class") as ICacheCategoryGenerator;
            if (CategoryGenerator == null)
                CategoryGenerator = ReflectionUtil.TryCreateInstance("BizElements.BusinessLayer.Aspects", "BizElements.BusinessLayer.Aspects.CacheCategoryGenerator") as ICacheCategoryGenerator;
        }
    }
}
